package cc.deng.wubo.minapp.config;

import cc.deng.wubo.minapp.component.FileProperties;
import org.nutz.filepool.NutFilePool;
import org.nutz.mvc.upload.UploadAdaptor;
import org.nutz.mvc.upload.UploadingContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

@Configuration
public class FileUploadConfig {


    @Autowired
    private FileProperties properties;

/*
    @Bean
    public CommonsMultipartResolver multipartResolver(){
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(104857600);
        multipartResolver.setMaxInMemorySize(4096);
        return multipartResolver;
    }*/

    @Bean
    public NutFilePool tempFilePool(){
        NutFilePool tempFilePool = new NutFilePool(properties.getTmppath(),properties.getFilemaxsize());
        return tempFilePool;
    }

    @Bean
    @Scope("prototype") //创建多实例
    public UploadingContext uploadImageContext(){
        UploadingContext uploadingContext = new UploadingContext(tempFilePool());
        uploadingContext.setIgnoreNull(true);
        uploadingContext.setMaxFileSize(2097152);
        uploadingContext.setNameFilter("^(.+[.])(gif|jpg|png)$");

        return uploadingContext;
    }

    @Bean
    @Scope("prototype")
    public UploadAdaptor imageUpload(){
        UploadAdaptor imageUpload = new UploadAdaptor(uploadImageContext());
        return imageUpload;
    }

    @Bean
    @Scope("prototype")
    public UploadingContext uploadFileContext(){
        UploadingContext uploadFileContext = new UploadingContext(tempFilePool());
        uploadFileContext.setIgnoreNull(true);
        uploadFileContext.setMaxFileSize(20971520);
        uploadFileContext.setNameFilter("^(.+[.])(gif|jpg|png|doc|docx|xls|xlsx|rar|zip|7z|flv|swf|mkv|avi|txt|xml|pdf|md|pptx|ppt|flv|swf|mkv|avi|rm|rmvb|wmv|mp4|mov|mpg|mpeg|class|jar)$");
        return uploadFileContext;
    }

    @Bean
    @Scope("prototype")
    public UploadAdaptor fileUpload(){
        UploadAdaptor fileUpload = new UploadAdaptor(uploadImageContext());
        return fileUpload;
    }

    @Bean
    @Scope("prototype")
    public UploadingContext uploadVideoContext(){
        UploadingContext uploadVideoContext = new UploadingContext(tempFilePool());
        uploadVideoContext.setIgnoreNull(true);
        uploadVideoContext.setMaxFileSize(209715200);
        uploadVideoContext.setNameFilter("^(.+[.])(flv|swf|mkv|avi|rm|rmvb|wmv|mp4|mov|mpg|mpeg)$");
        return uploadVideoContext;
    }

    @Bean
    @Scope("prototype")
    public UploadAdaptor videoUpload(){
        UploadAdaptor videoUpload = new UploadAdaptor(uploadVideoContext());
        return videoUpload;
    }


}
